<?php
$title = "회원관리";
$menu = 0;
require_once ('./admin.head.php');
        $_GET['division'] = isset($_GET['division']) ? $_GET['division'] : 'student';
        $_GET['condition'] = isset($_GET['condition']) ? $_GET['condition'] : '';
        $_GET['page'] = isset($_GET['page'])? $_GET['page']: 1;

    function conditionSQLBuilder() {
        $fieldName = '';
        $keyword = isset($_GET['keyword']) ? $_GET['keyword']: '';
        if(isset($_GET['condition']) and in_array($_GET['condition'], array('telNum', 'name', 'birth'))) {
            switch ($_GET['condition']) {
                case 'telNum':
                    return "mb_tel like '%$keyword%'";
                break;
                case 'name':
                    return "mb_name like '%$keyword%'";
                break;
                case 'birth':
                    return "mb_birth like '%$keyword%'";
                break;

            }
        }

    }
    
    function divisionSQLBuilder() {
        $division = array();
        $_GET['division'] = $_GET['division'] ? $_GET['division'] : 'student';
        switch ($_GET['division']) {
            case "student":
                $division[] = "'학생'";
            break;
            case "teacher":
                $division[] = "'강사'";
            break;
            default : 
            $division = array("'학생'","'강사'");
            break;
        }
    	return ' mb_division IN ('.implode(' , ', $division).') ';
    }

$allDivisionChecked = $teacherDivisionChecked = $studentDivisionChecked = '';
	switch ($_GET['division']) {
		case 'teacher':
			$teacherDivisionChecked = 'checked = "checked"';
		break;
        case 'student':
			$studentDivisionChecked = 'checked = "checked"';
        break;
	/*	default : 
            $allDivisionChecked = 'checked = "checked"';
		break; */
    }
	
$nameSelected = $birthSelected = $telNumSelected = '';
if(isset($_GET['condition'])) {
	switch ($_GET['condition']) {
		case 'birth':
			$birthSelected = 'selected = "selected"';
		break;			
		case 'telNum':
			$telNumSelected = 'selected = "selected"'; 
		break;
		case 'name':
			$nameSelected = 'selected = "selected"';
		break;
        default : 
            $selectSelected = 'selected = "selected"';
        break;
	}
}

    $divisionWhere = divisionSQLBuilder();
    $conditionWhere = conditionSQLBuilder();
    
    $where = getAndWhere(array($divisionWhere, $conditionWhere));
    
    $limitSQL = getLimit();
    $dataSql = "SELECT * FROM {$g4['member_table']} WHERE $where $limitSQL";
    $countSql = "SELECT count(1) FROM {$g4['member_table']} WHERE $where";
    $i = 0;
    $res = mysql_query($dataSql);
    list($totalCount) = mysql_fetch_array(mysql_query($countSql));
    $totalCount -= ($_GET['page'] - 1) * 20;
    
    $datas = array();
    while($data = mysql_fetch_assoc($res)) {
            $datas[] = $data;
    }

    $links = getPager($totalCount);
    $tpl->define('basic', 'members.html');
    $tpl->print_('basic');
    require_once ('./admin.foot.php');
?>
